package k.web.cityManage;

import com.alibaba.fastjson.JSON;
import k.HelperClazz;
import k.dao.base.OrgDao;
import k.dto.Org;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by sure on 2018/4/12.
 */
@Controller
@RequestMapping(value = "/org")
public class OrgController {
    @Autowired
    private OrgDao orgDao;
    /**
     * 部门管理主页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/orgIndex")
    public String index(Model model, HttpServletRequest request) {
        model.addAttribute("emp_id", HelperClazz.getEMP().getEmp_id());
        model.addAttribute("emp_name", HelperClazz.getEMP().getEmp_name());
        //查询所有部门
        model.addAttribute("orgs",orgDao.findAll());
        return "system_manage/orgIndex";
    }

    @RequestMapping(value = "/insertOrg")
    public String insertOrg(Org org) {
        if(org.getOrg_id()  == null){
            //新增
            orgDao.insertOrg(org);
        }else{
            //修改
            orgDao.upOrg(org);
        }
        return "redirect:/org/orgIndex";
    }

    @RequestMapping(value = "/queryOrg")
    public void queryOrg(String orgId,HttpServletResponse response){
        HelperClazz.renderJson(response, JSON.toJSONString( orgDao.querybyId(orgId)));
    }


    /**
     * 删除org
     * @param orgId
     */
    @RequestMapping(value = "/delOrg")
    public String delOrg(String orgId,HttpServletResponse response) {
        orgDao.del(orgId);
        return "redirect:/emp/empIndex";
    }

}
